package com.zues.samples.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zues.samples.dto.CommonMessageResultVO;
import com.zues.samples.model.ExamTestType;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.Subject;
import com.zues.samples.model.Testmodel;
import com.zues.samples.service.ExamTestTypeService;
import com.zues.samples.service.SubjectService;
import com.zues.samples.service.TestmodelService;
import com.zues.samples.service.modelVO.ExamTestOV;

@Controller
@RequestMapping(value = "teacher")
public class TestController {

	@Autowired
	private TestmodelService testmodelService;
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private ExamTestTypeService examTestTypeService;
	

	@RequestMapping(value = "toManageTest", method = RequestMethod.GET)
	private String testList(Model model) throws Exception {
		PagerModel<ExamTestOV> list = testmodelService.findAllByPage();
		model.addAttribute("testModelList", list);
		return "teacher/ManageTest";
	}
	
	@RequestMapping(value="/saveTest",method = RequestMethod.POST)
	@ResponseBody
	private CommonMessageResultVO saveTest(Testmodel testmodel,BindingResult bindingResult){
		Map<String,Object> messageMap = new HashMap<String,Object>();
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error:errorList ){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
				CommonMessageResultVO resultVO = new CommonMessageResultVO(false, messageMap);
				return resultVO;
			}
		}
		try{
			testmodel.setSubject(subjectService.getByIdSubject(testmodel.getSubId()));
			testmodel.setExamTestType(examTestTypeService.getByIdExamTestType(testmodel.getExamTestTypeId()));
			testmodelService.createTest(testmodel);
			CommonMessageResultVO resultVO = new CommonMessageResultVO(true, messageMap);
			return resultVO;
		}catch (Exception e) {
			e.printStackTrace();
			messageMap.put("errMsg", e.getMessage());
			CommonMessageResultVO resultVO = new CommonMessageResultVO(false, messageMap);
			return resultVO;
		}
	}
	
	@RequestMapping(value="/editTest/{testId}",method = RequestMethod.POST)
	@ResponseBody
	private CommonMessageResultVO editTest(Testmodel testmodelEdit,BindingResult bindingResult, @PathVariable int testId){
		Map<String,Object> messageMap = new HashMap<String,Object>();
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error:errorList ){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
				CommonMessageResultVO resultVO = new CommonMessageResultVO(false, messageMap);
				return resultVO;
			}
		}
		try{
			//testmodelEdit.setId(testId);
			testmodelEdit.setSubject(subjectService.getByIdSubject(testmodelEdit.getSubId()));
			testmodelEdit.setExamTestType(examTestTypeService.getByIdExamTestType(testmodelEdit.getExamTestTypeId()));
			testmodelService.editTest(testmodelEdit);
			CommonMessageResultVO resultVO = new CommonMessageResultVO(true, messageMap);
			return resultVO;
		}catch (Exception e) {
			e.printStackTrace();
			messageMap.put("errMsg", e.getMessage());
			CommonMessageResultVO resultVO = new CommonMessageResultVO(false, messageMap);
			return resultVO;
		}
	}
	
	
	@RequestMapping(value = "/deleteTest/{testId}", method = RequestMethod.GET)
	private String deleteTest(RedirectAttributes redirectAttributes, @PathVariable int testId){
		try {
			testmodelService.deleteTest(testId);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/teacher/toManageTest";
	}
	
	
	@RequestMapping(value = "/getSubjectAll", method = RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> getSubjectAll(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			List<Subject> subjectlist = subjectService.findAllSubjects();
			modelMap.put("subjectlist", subjectlist);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getTestTypeAll", method = RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> getTestType(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			List<ExamTestType> examTestTypelist = examTestTypeService.FindExamTestType();
			modelMap.put("examTestTypelist", examTestTypelist);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getByIdSubject/{subId}", method = RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> getByIdSubject(HttpServletRequest request, @PathVariable int subId){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			Subject subjectByOneId = subjectService.getByIdSubject(subId);
			modelMap.put("subjectByOneId", subjectByOneId);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getByIdTest/{testId}", method = RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> getByIdTestType(HttpServletRequest request, @PathVariable int testId){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			ExamTestOV testmodelByOneId = testmodelService.getTestByOneId(testId);
			modelMap.put("testmodelByOneId", testmodelByOneId);
			modelMap.put("success", true);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}
}
